<?php
// 校对函数

/**
 * mobile
 * @param  string $str mobile string
 * @param  boolean $contain contain model
 * @return boolean      is match
 */
function match_mobile($str, $contain=false){
    if ($contain) {
        $pattern = '/1[34578]\d{9}/';
    } else {
        $pattern = '/^1[34578]\d{9}$/';
    }
    return preg_match($pattern, $str) === 1;
}

/**
 * phone
 * @param  string $str phone string
 * @param  boolean $contain contain model
 * @return boolean      is match
 */
function match_phone($str, $contain=false){
    if ($contain) {
        $pattern = '/(\d{3}-\d{8})|(\d{4}-\d{7})/';
    } else {
        $pattern = '/^(\d{3}-\d{8})|(\d{4}-\d{7})$/';
    }
    return preg_match($pattern, $str) === 1;
}

/**
 * email match
 * @param  string $str email string
 * @param  boolean $contain contain model
 * @return boolean    is match
 */
function match_email($str, $contain=false){
    if ($contain) {
        $pattern = '/(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})/i';
    } else {
        $pattern = '/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/i';
    }
    return preg_match($pattern, $str) === 1;
}

/**
 * http match,if you shoud,you can change suffix
 * https://www.baidu.com
 * @param  string $str http string
 * @param  boolean $contain contain model
 * @return boolean      is match
 */
function match_http($str, $contain=false){
    if ($contain) {
        $pattern = '/http(s)?:\/\/([a-z0-9]+(-[a-z0-9]+)?\.)+((com)|(cn)|(net)|(com\.cn)|(org))/i';
    } else {
        $pattern = '/^http(s)?:\/\/([a-z0-9]+(-[a-z0-9]+)?\.)+((com)|(cn)|(net)|(com\.cn)|(org))$/i';
    }
    return preg_match($pattern, $str) === 1;
}

/**
 * 身份证 match
 * @param  string $str IDcard string
 * @param  boolean $contain contain model
 * @return boolean      is match
 */
function match_IDcard($str, $contain=false){
    if ($contain) {
        $pattern = '/(\d{15})|(\d{18})/';
    } else {
        $pattern = '/^(\d{15})|(\d{18})$/';
    }
    return preg_match($pattern, $str) === 1;
}

/**
 * Chinese
 * @param  string $str chinese string
 * @param  boolean $contain contain model
 * @return boolean      is match
 */
function match_Chinese($str, $contain=false){
    if ($contain) {
        $pattern = '/[\x{4e00}-\x{9fa5}]+/';
    } else {
        $pattern = '/^[\x{4e00}-\x{9fa5}]+$/';
    }
    return preg_match($pattern, $str) === 1;
}

/**
 * qq,最短5位,最长12位
 * @param  string $str qq string
 * @param  boolean $contain contain model
 * @return boolean      is match
 */
function match_QQ($str, $contain=false){
    if ($contain) {
        $pattern = '/[1-9][0-9]{4,11}/';
    } else {
        $pattern = '/^[1-9][0-9]{4,11}$/';
    }
    return preg_match($pattern, $str) === 1;
}

/**
 * PostCode 邮编
 * @param  string $str PostCode string
 * @param  boolean $contain contain model
 * @return boolean      is match
 */
function match_PostCode($str, $contain=false){
    if ($contain) {
        $pattern = '/[1-9]\d{5}(?!\d)/';
    } else {
        $pattern = '/^[1-9]\d{5}(?!\d)$/';
    }
    return preg_match($pattern, $str) === 1;
}

/**
 * IP
 * @param  string $str IP string
 * @param  boolean $contain contain model
 * @return boolean      is match
 */
function match_IP($str, $contain=false){
    if ($contain) {
        $pattern  = '/(((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}';
        $pattern .= '(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))/';
    } else {
        $pattern  = '/^(((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}';
        $pattern .= '(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))$/';
    }
    return preg_match($pattern, $str) === 1;
}
